<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        /* 
            类型转换值将一种数据类型转换为其他类型
                将其他类型转换为字符串、数值、布尔值等等

            转换为字符串
                toString()
                String()
        */
        let a = 123;
        a = a.toString();  // 不能对null进行转换
        console.log(typeof a, a);
        a = String(a);  // 可以对null进行转换
        console.log(typeof a, a);

        a = 123;
        a = a + "";
        console.log(typeof a, a);  // string 123
        /*
            转换为数值
                Number()
        */
        let b = '123';
        b = Number(b);
        console.log(typeof b, b);  // 123

        b = "abc"
        b = Number(b);
        console.log(typeof b, b);  // NaN

        b = ""
        b = Number(b);
        console.log(typeof b, b);  // 0

        b = null
        b = Number(b);
        console.log(typeof b, b);  // 0

        b = undefined
        b = Number(b);
        console.log(typeof b, b);  // NaN

        b = true
        b = Number(b);
        console.log(typeof b, b);  // 1

        b = false
        b = Number(b);
        console.log(typeof b, b);  // 0

        b = "1234a5bc"
        b = parseInt(b);
        console.log(typeof b, b);
        /*
            转换为布尔值
                Boolean()
        */
        let c = 0;
        c = Boolean(c);
        console.log(typeof c, c);  // false

        c = 1;
        c = Boolean(c);
        console.log(typeof c, c);  // true

        c = -1;
        c = Boolean(c);
        console.log(typeof c, c);  // true

        c = 2.1;
        c = Boolean(c);
        console.log(typeof c, c);  // true


    </script>
</head>

<body>

</body>

</html>